package com.itheima.dbservice;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.mapper.SettingApiMapper;
import com.itheima.pojo.Settings;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;

@DubboService
public class SettingServiceApiImpl implements SettingServiceApi {
    @Autowired
    private SettingApiMapper settingApiMapper;

    /**
     * 通知设置
     */
    public void settingNotice(Long uid, Map<String, Boolean> map) {
        //构造条件
        LambdaQueryWrapper<Settings> wrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Settings> eq = wrapper.eq(Settings::getUserId, uid);
        //查询原有通知
        Settings settingsOne = settingApiMapper.selectOne(eq);
        //判断修改的是哪个设置
        //将数据进行封装
        settingsOne.setLikeNotification(map.get("likeNotification") ? 0 : 1);
        settingsOne.setPinglunNotification(map.get("pinglunNotification") ? 0 : 1);
        settingsOne.setGonggaoNotification(map.get("gonggaoNotification") ? 0 : 1);
        settingsOne.setUpdated(new Date(System.currentTimeMillis()));
        //执行sql
        settingApiMapper.update(settingsOne, eq);
    }


    //获取通知做回显
    @Override
    public Settings getSettingsApi(Long uid) {
        //构造条件
        LambdaQueryWrapper<Settings> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Settings::getUserId, uid);
        //查询是否存在问题
        Settings settings = settingApiMapper.selectOne(wrapper);
        if (ObjectUtil.isNull(settings)) {
            //若不存在,则设置默认
            Settings settings1 = new Settings();
            settings1.setGonggaoNotification(1);
            settings1.setLikeNotification(1);
            settings1.setPinglunNotification(1);
            settings1.setCreated(new Date(System.currentTimeMillis()));
            settings1.setUpdated(new Date(System.currentTimeMillis()));
            settings1.setUserId(uid);
            //存储db中
            settingApiMapper.insert(settings1);
            return settings1;
        }
        return settings;
    }
}
